package com.db.stockexchange.client.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.db.stockexchange.client.ClientPresenter;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
import net.miginfocom.swing.MigLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class LoginDialog extends JDialog {
	private static final long serialVersionUID = 3081973283647168668L;
	
	private final JPanel contentPanel = new JPanel();
	private JTextField txtAddress;
	private JTextField txtPort;
	private JTextField txtName;

	private ClientPresenter presenter;


	/**
	 * Create the dialog.
	 */
	public LoginDialog(final ClientPresenter presenter, final JFrame parent) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				parent.dispose();
			}
		});
		this.presenter = presenter;
		
		setBounds(100, 100, 312, 159);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[100px][200px]", "[26px][26px][26px]"));
		{
			JLabel lblAddress = new JLabel("Address");
			contentPanel.add(lblAddress, "cell 0 0,grow");
		}
		{
			txtAddress = new JTextField("127.0.0.1");
			contentPanel.add(txtAddress, "cell 1 0,growx,aligny center");
			txtAddress.setColumns(20);
		}
		{
			JLabel lblPort = new JLabel("Port");
			contentPanel.add(lblPort, "cell 0 1,grow");
		}
		{
			txtPort = new JTextField("5090");
			contentPanel.add(txtPort, "cell 1 1,growx,aligny center");
			txtPort.setColumns(20);
		}
		{
			JLabel lblName = new JLabel("Name");
			contentPanel.add(lblName, "cell 0 2,grow");
		}
		{
			txtName = new JTextField("Default");
			contentPanel.add(txtName, "cell 1 2,growx,aligny center");
			txtName.setColumns(20);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						InetAddress address = InetAddress.getLoopbackAddress();
						int port;
						try {
							address = InetAddress.getByName(txtAddress.getText());
							port = Integer.parseInt(txtPort.getText());
							if (port < 1024 || port > 65535) 
								throw new NumberFormatException("Port number must be between 1024 and 65535");
										
							if (!LoginDialog.this.presenter.login(address, port, txtName.getText())) {
								JOptionPane.showMessageDialog(contentPanel, String.format("Could not connect to server by address %s:%d", address, port));
							} else {
								LoginDialog.this.dispose();
							}
							
						} catch (UnknownHostException ex) {
							JOptionPane.showMessageDialog(contentPanel, "Address is illegal. " + ex.getMessage());
							return;
						} catch (NumberFormatException ex) {
							JOptionPane.showMessageDialog(contentPanel, "Port is illegal. " + ex.getMessage());
							return;
						}
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						parent.dispose();
						LoginDialog.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
